package application.services;

import application.exceptions.ItemNotFoundException;
import application.model.PhasesModel;
import application.model.modify.PhaseModel;
import application.translators.Translator;
import application.translators.impl.PhasesTranslator;
import dao.PhaseDao;
import mappings.Phases;
import org.hibernate.exception.ConstraintViolationException;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.List;

@Path("phases")
public class PhasesService
{
    private PhaseDao phaseDao = new PhaseDao();
    private Translator<List<Phases>, PhasesModel> translator = new PhasesTranslator();

    @Path("getAllPhases")
    @GET
    @Produces(MediaType.APPLICATION_XML)
    public PhasesModel getAllPhases()
    {
        return translator.translate(phaseDao.getAllPhases());
    }

    @Path("addPhase")
    @POST
    @Produces(MediaType.APPLICATION_XML)
    public PhaseModel addNewProducent(@FormParam("name") String name)
    {
        PhaseModel phaseModel = new PhaseModel();

        try
        {
            Phases phase = phaseDao.addPhase(name);
            phaseModel.setId(phase.getId());
            phaseModel.setName(phase.getName());
            phaseModel.setStatus(0);
        }
        catch (ConstraintViolationException e)
        {
            // lapiemy wyjatek rzucany przez hibernate'a dla unique constraint - ustawiamy status 1 (rekord istnieje)
            phaseModel.setStatus(1);
        }
        catch (Exception e)
        {
            // lapiemy wszystkie inne exceptiony i ustawiamy status na 2 (czyli cos sie wyjebalo, ale nie wiadomo co :P)
            phaseModel.setStatus(2);
        }

        return phaseModel;
    }

    @Path("editPhase")
    @POST
    @Produces(MediaType.APPLICATION_XML)
    public PhaseModel editPhase(@FormParam("id") Integer id, @FormParam("name") String name)
    {
        PhaseModel phaseModel = new PhaseModel();

        try
        {
            Phases phase = phaseDao.editPhase(id, new Phases(name));
            phaseModel.setName(phase.getName());
            phaseModel.setId(phase.getId());
            phaseModel.setStatus(0);
        }
        catch (ItemNotFoundException e)
        {
            phaseModel.setStatus(1); // 1 = nie znaleziono rekordu o danym id
        }
        catch (Exception e)
        {
            phaseModel.setStatus(2);
        }

        return phaseModel;
    }

    @Path("deletePhase")
    @POST
    @Produces(MediaType.APPLICATION_XML)
    public PhaseModel deletePhase(@FormParam("id") Integer id)
    {
        PhaseModel phaseModel = new PhaseModel();

        try
        {
            phaseDao.deletePhase(id);
            phaseModel.setStatus(0);
        }
        catch (ItemNotFoundException e)
        {
            phaseModel.setStatus(1); // 1 = nie znaleziono rekordu o danym id
        }
        catch (Exception e)
        {
            phaseModel.setStatus(2);
        }

        return phaseModel;
    }
}
